//
//  ZJMineThroughRecordInfoMoreViewTableViewCell.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/17.
//

#import "ZJMineThroughRecordInfoMoreViewTableViewCell.h"
#import "ZJMineThroughRecordInfoCollectionViewCell.h"

@interface ZJMineThroughRecordInfoMoreViewTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray * selectArr;
@end



@implementation ZJMineThroughRecordInfoMoreViewTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.dataArray = @[].mutableCopy;
        [self setupUI];
    }
    return self;
}


-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    self.selectArr = @[].mutableCopy;
    [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.selectArr addObject:@"0"];
    }];
    
    
    
}


- (void)setupUI {
    // 创建布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // 创建 CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionView];
    
    // 注册 Cell
    [self.collectionView registerClass:[ZJMineThroughRecordInfoCollectionViewCell class] 
              forCellWithReuseIdentifier:@"ZJMineThroughRecordInfoCollectionViewCell"];
    
    // 设置约束（假设使用 Masonry）
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

// 添加必要的数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZJMineThroughRecordInfoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZJMineThroughRecordInfoCollectionViewCell" forIndexPath:indexPath];
    
    cell.msglabel.text =  self.dataArray[indexPath.row];
    if ([self.selectArr[indexPath.row] isEqualToString:@"0"]){
        cell.msglabel.backgroundColor = COLOR_RGB(244, 244, 244);
        cell.msglabel.textColor = COLOR_RGB(102, 102, 102);
    }else{
        cell.msglabel.backgroundColor = COLOR_RGB(50, 113, 237);
        cell.msglabel.textColor = [UIColor whiteColor];
    }
    
    
    
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.selectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.selectArr replaceObjectAtIndex:idx withObject:@"0"];
    }];
    
    [self.selectArr replaceObjectAtIndex:indexPath.row withObject:@"1"];
    [self.collectionView reloadData];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseTypeWithString:)]){
        [self.delegate chooseTypeWithString:self.dataArray[indexPath.row]];
    }
    
    
    
    
}


@end
